// 基础配置
const baseConfig = {
	baseURL: 'http://localhost:8084', // 使用 HTTPS
	timeout: 10000, // 超时时间，单位ms
	header: {
		'Content-Type': 'application/json'
	},
	sslVerify: false // 忽略证书验证
}


// 统一请求方法
const request = (options) => {
	// 合并配置
	const config = {
		...baseConfig,
		...options,
		url: baseConfig.baseURL + options.url,
		header: {
			...baseConfig.header,
			...options.header
		}
	}
	
	return new Promise((resolve, reject) => {
		uni.request({
			...config,
			success: (res) => {
				console.log('请求成功：', res);
				if (res.statusCode >= 200 && res.statusCode < 300) {
					resolve(res.data);
				} else {
					uni.showToast({
						title: res.data.message || '请求失败',
						icon: 'none'
					});
					reject(res);
				}
			},
			fail: (err) => {
				console.error('请求失败：', err);
				// 添加更详细的错误信息
				if (err.errMsg.includes('SSL')) {
					uni.showToast({
						title: 'SSL证书验证失败，请检查证书配置',
						icon: 'none',
						duration: 3000
					});
				} else {
					uni.showToast({
						title: '网络请求失败',
						icon: 'none'
					});
				}
				reject(err);
			}
		});
	});
}

// 封装常用请求方法
const http = {
	get: (url, data = {}, options = {}) => {
		return request({
			url,
			data,
			method: 'GET',
			...options
		})
	},
	
	post: (url, data = {}, options = {}) => {
		return request({
			url,
			data,
			method: 'POST',
			...options
		})
	},
	
	put: (url, data = {}, options = {}) => {
		return request({
			url,
			data,
			method: 'PUT',
			...options
		})
	},
	
	delete: (url, data = {}, options = {}) => {
		return request({
			url,
			data,
			method: 'DELETE',
			...options
		})
	}
}

export default http 